import { message } from 'antd';
import { history } from 'umi';
import { addShop, fetchShop,updateShop } from './service';

const Model = {
  namespace: 'cubeShop',
  state: {
    currentShop:{},
  },
  effects: {
    *add({ payload }, { call }) {
      let result=yield call(addShop, payload);
      if(result.code===0){
        message.success('提交成功');
        history.push('/shop/center')
      }else{
        message.error(result.message);
      }
    },
    *update({ payload }, { call }) {
      let result=yield call(updateShop, payload);
      if(result.code===0){
        message.success('提交成功');
        history.push('/shop/center')
      }else{
        message.error(result.message);
      }
    },
    *fetchShop({ payload }, { call,put }) {
      let result=yield call(fetchShop, payload);
      if(result.code===0){
        let _params={
          addressArr:[],
          ...result.data
        }
        if(result.data.provincial){
          _params.addressArr[0]=Number(result.data.provincial)
        }
        if(result.data.city){
          _params.addressArr[1]=Number(result.data.city)
        }
        if(result.data.district){
          _params.addressArr[2]=Number(result.data.district)
        }
        yield put({
          type: 'common',
          payload: {
            currentShop:_params
          }
        });
      }else{
        message.error(result.message);
      }
    },
  },
  reducers: {
    save(state, {payload}) {
      return {
        ...state,
        ...action.payload,
      };
    },
    common(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};
export default Model;
